package com.uwo.commons.kits;

import java.io.*;
import java.util.Collection;

/**
 * 文件工具
 * @author hao.yan
 */
public class FileKits {

    /**
     * 通过地址读取行
     * @param path
     * @param contexts
     * @throws Exception
     */
    public static void readLine(String path, Collection<String> contexts) throws Exception{
        readLine(new File(path), contexts);
    }

    /**
     * 通过文件读取行
     * @param file
     * @return
     * @throws Exception
     */
    public static void readLine(File file, Collection<String> contexts) throws Exception {
        // 判断文件是否存在
        if(!file.isFile() && !file.exists())
            return;
        readLine(new FileInputStream(file), contexts);
    }

    /**
     * 通过文件读取行
     * @param in
     * @param contexts
     * @throws Exception
     */
    public static void readLine(InputStream in, Collection<String> contexts) throws Exception {
        // 不重复设置敏感词
        InputStreamReader read = new InputStreamReader(in, "UTF-8");
        try {
            BufferedReader bufferedReader = new BufferedReader(read);
            String txt = null;
            while((txt = bufferedReader.readLine()) != null){    //读取文件，将文件内容放入到set中
                contexts.add(txt);
            }
        } catch (Exception e) {
            throw e;
        }finally{
            read.close();     //关闭文件流
        }
    }

}
